package com.pox.guli.service.vod.controller.admin;

import com.pox.guli.common.base.result.R;
import com.pox.guli.common.base.result.ResultCodeEnum;
import com.pox.guli.common.base.util.ExceptionUtils;
import com.pox.guli.service.base.exception.PoXException;
import com.pox.guli.service.vod.service.VodVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author PoX21s
 * @Date: 2022/1/5 13:25
 * @Version 1.0
 */

@Api(description = "阿里云视频点播")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/vod/media")
@Slf4j
public class MediaController {

    @Resource
    private VodVideoService vodVideoService;

    @ApiOperation("视频上传")
    @PostMapping("/upload")
    public R uploadVideo(@ApiParam(name = "file", value = "文件", required = true)
                                    @RequestParam("file") MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String videoId = vodVideoService.uploadVideo(inputStream, originalFilename);

            return R.ok().message("视频上传成功").data("videoId", videoId);
        } catch (IOException e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new PoXException(ResultCodeEnum.VIDEO_UPLOAD_TOMCAT_ERROR);
        }
    }


    @ApiOperation("视频删除")
    @DeleteMapping("/remove/{vodId}")
    public R removeVideo(@ApiParam(value = "阿里云视频id", required = true)
                         @PathVariable String vodId) {

        try {
            vodVideoService.removeVideo(vodId);

            return R.ok().message("视频删除成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));

            throw new PoXException(ResultCodeEnum.VIDEO_DELETE_ALIYUN_ERROR);
        }
    }
}
